package dao;

import java.util.Calendar;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

import entity.TreatmentRequest;

@JPA
@Stateless
public class JPATreatmentRequestDAO extends GenericJPADAO<TreatmentRequest> implements TreatmentRequestDAO {

	@Override
	public List<String> selectDoctorsNamesByActId(int actId) {
		List<String> res = null;
		try {
			TypedQuery<String> query = em.createNamedQuery("TreatmentRequest.selectDoctorNamesByActId", String.class).setParameter("actId", actId);
			res = query.getResultList();
		} catch(NoResultException nre) {}
		return res;				
	}	
	
	@Override
	public List<TreatmentRequest> selectRequestsByDateRange(Calendar startDate, Calendar endDate) {
		List<TreatmentRequest> res = null;		
		endDate = (Calendar) endDate.clone(); 
		endDate.add(Calendar.DAY_OF_YEAR, 1);		
		try {
			TypedQuery<TreatmentRequest> query = em.createNamedQuery("TreatmentRequest.selectRequestsByDateRange", TreatmentRequest.class)
					.setParameter("startDate", startDate, TemporalType.TIMESTAMP)
					.setParameter("endDate", endDate, TemporalType.TIMESTAMP);
			res = query.getResultList();
		} catch(NoResultException nre) {}
		return res;				
	}
	
	@Override
	public List<TreatmentRequest> selectRequestsByDoctorIdAndDateRange(int doctorId, Calendar startDate, Calendar endDate) {
		List<TreatmentRequest> res = null;		
		endDate = (Calendar) endDate.clone(); 
		endDate.add(Calendar.DAY_OF_YEAR, 1);		
		try {
			TypedQuery<TreatmentRequest> query = em.createNamedQuery("TreatmentRequest.selectRequestsByDoctorIdAndDateRange", TreatmentRequest.class)
					.setParameter("doctorId", doctorId).setParameter("startDate", startDate, TemporalType.TIMESTAMP)
					.setParameter("endDate", endDate, TemporalType.TIMESTAMP);
			res = query.getResultList();
		} catch(NoResultException nre) {}
		return res;				
	}	
	
}